package com.lagou.zkboot.web.service;

import com.lagou.zkboot.web.config.DataSourceConfig;
import com.lagou.zkboot.web.config.DataSourceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Service
public class TestService {

    @Autowired
    private DataSourceUtils dataSourceUtils;

    public void reloadDB() throws Exception {
        DataSourceConfig dataSourceConfig = dataSourceUtils.getDataSourceConfig();
        dataSourceConfig.setMinIdle(3);
        dataSourceUtils.updateDataSourceConfig(dataSourceConfig);
    }

    public int queryCount() throws SQLException {
        Connection connection = dataSourceUtils.getDataSource().getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select count(1) from tb_resume");
        if (resultSet.next()){
            return resultSet.getInt(1);
        }
        return -1;
    }
}
